package com.visionvera.myapplication.net;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.text.TextUtils;

import com.trello.rxlifecycle2.LifecycleProvider;
import com.visionvera.retrofitservice.net.BaseEntity;
import com.visionvera.retrofitservice.net.BaseObserver;
import com.visionvera.retrofitservice.net.NetworkRequest;
import com.visionvera.retrofitservice.utils.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by dongyukun on 2017/11/30.
 */

public class APIServer extends NetInstance {

    /**
     * 登陆
     */
    public static void doLogin(LifecycleProvider lifecycleProvider, String phone, String password, String uuid, BaseObserver<BaseEntity<LoginBean>> subscriber) {

//        NetworkRequest networkRequest = NetworkRequest.getInstance();
//        APIFunction apiFunction = (APIFunction) NetworkRequest.getInstance().getServiceInstance();

        Map<String, String> map = new HashMap<String, String>();
        map.put("phone", phone);
        map.put("password", password);
        map.put("deviceId", uuid);
        getNetworkRequest().subscribe(lifecycleProvider, getApiFunction().doLogin(map), subscriber);
//        RetrofitFactory.getInstance().API().getBaidu().doHttpRequest(
//                HttpManager.getInstance(host).getHttpService()
//                        .doLogin(map)
//                        .map(new BaseEntity<LoginBean>())
//                        .compose(ac.bindToLifecycle())
//                , resultSubscriber);
//
//        RetrofitFactory.getInstance().API()
//                .getBaidu("我是中国人")
//                .compose(ac.bindToLifecycle())
//                .subscribe(resultSubscriber);

//        RetrofitFactory.getInstance().API()
//                .getBaidu("我是中国人")
//                .compose(ac.bindToLifecycle())
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(resultSubscriber);


    }

    /**
     * 上传单张图片
     *
     * @param filePath 图片路径
     * @throws IOException
     */
    @SuppressLint("StaticFieldLeak")
    public static void uploadImage(final LifecycleProvider lifecycleProvider, final String id, final String filePath, final BaseObserver subscriber) {
        new AsyncTask<Integer, Integer, String>() {
            @Override
            protected String doInBackground(Integer... params) {
                //压缩图片
//                File file = new File(BitmapUtil.compressImage(""));
                return filePath;
            }

            @Override
            protected void onPostExecute(String file) {
                super.onPostExecute(file);
//                RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
//                MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
//
//                // 创建 RequestBody，用于封装构建RequestBody
//                RequestBody requestFile =
//                        RequestBody.create(MediaType.parse("multipart/form-data"), file);
//
//                MultipartBody.Part body =
//                        MultipartBody.Part.createFormData("image", file.getName(), requestFile);
//
//                String descriptionString = "hello, 这是文件描述";
//                RequestBody description =
//                        RequestBody.create(
//                                MediaType.parse("multipart/form-data"), descriptionString);
//
//                RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//                MultipartBody.Part imageBodyPart = MultipartBody.Part.createFormData("file", file.getName(), imageBody);
//                RequestBody tokenBody = RequestBody.create(MediaType.parse("text/plain"), id);

//                MultipartBody.Builder builder = new MultipartBody.Builder().setType(MediaType.parse("multipart/form-data"))
//                        .addFormDataPart("id", id)
//                        .addFormDataPart("phone", "")
//                        .addFormDataPart("app_token", "");
//                RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpg"), FileUtils.scalePhoto(filePath));
//                MultipartBody.Part imageBodyPart = MultipartBody.Part.createFormData("file", "avatar.jpg", requestBody);
//                builder.addPart(imageBodyPart);

                File files = new File(filePath);//filePath 图片地址
                String token = "ASDDSKKK19990SDDDSS";//用户token
                MultipartBody.Builder builder = new MultipartBody.Builder()
                        .setType(MultipartBody.FORM)//表单类型
                        .addFormDataPart("id", id)//ParamKey.TOKEN 自定义参数key常量类，即参数名
                        .addFormDataPart("token", token);//ParamKey.TOKEN 自定义参数key常量类，即参数名
                RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), files);
                builder.addFormDataPart("file", files.getName(), imageBody);//imgfile 后台接收图片流的参数名

                List<MultipartBody.Part> parts = builder.build().parts();

//                Map<String, RequestBody> map = new HashMap<String, RequestBody>();
//                map.put("id",  RequestBody.create(MediaType.parse("form-data"), id));
//                map.put("token", RequestBody.create(MediaType.parse("form-data"), "" ));
//                map.put("phone", RequestBody.create(MediaType.parse("form-data"),"" ));
//                File f = new File(filePath);
//                final MultipartBody.Builder builder = new MultipartBody.Builder();
//                builder.addFormDataPart("file",f.getName(), RequestBody.create(MultipartBody.FORM,f));

//                RetrofitFactory.getInstence().API()
//                        .imageUpload( part)
//                        .subscribeOn(Schedulers.io())
//                        .observeOn(AndroidSchedulers.mainThread())
//                        .subscribe(observer);
                getNetworkRequest().subscribe(lifecycleProvider, getApiFunction().imageUpload(parts), subscriber);
            }
        }.execute();
    }
}
